package com.ruoyi.user.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.ThisUser;
import com.ruoyi.user.service.IThisUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 客户Controller
 * 
 * @author yg
 * @date 2023-11-23
 */
@Controller
@RequestMapping("/user/user")
public class ThisUserController extends BaseController
{
    private String prefix = "user/user";

    @Autowired
    private IThisUserService thisUserService;

    @RequiresPermissions("user:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询客户列表
     */
    @RequiresPermissions("user:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ThisUser thisUser)
    {
        startPage();
        List<ThisUser> list = thisUserService.selectThisUserList(thisUser);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("user:user:export")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ThisUser thisUser)
    {
        List<ThisUser> list = thisUserService.selectThisUserList(thisUser);
        ExcelUtil<ThisUser> util = new ExcelUtil<ThisUser>(ThisUser.class);
        return util.exportExcel(list, "客户数据");
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("user:user:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ThisUser thisUser, HttpServletRequest request)
    {
        String name = (String) request.getSession().getAttribute("user");
        thisUser.setCreateBy(name);
        thisUser.setUpdateBy(name);
        return toAjax(thisUserService.insertThisUser(thisUser));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("user:user:edit")
    @GetMapping("/edit/{uId}")
    public String edit(@PathVariable("uId") Long uId, ModelMap mmap)
    {
        ThisUser thisUser = thisUserService.selectThisUserByUId(uId);
        mmap.put("thisUser", thisUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("user:user:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ThisUser thisUser)
    {
        return toAjax(thisUserService.updateThisUser(thisUser));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("user:user:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(thisUserService.deleteThisUserByUIds(ids));
    }
}
